package com.vagavaga.bizworld.jpatx;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.persistence.EntityTransaction;

class ParentTransaction extends EntityTransactionInterceptor {
    private final EntityTransaction tx;

    private ParentTransaction(EntityTransaction tx) {
        this.tx = tx;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            return method.invoke(tx, args);
        } catch (InvocationTargetException ite) {
            throw ite.getTargetException();
        }
    }

    public static EntityTransaction wrap(EntityTransaction transaction) {
        ParentTransaction tx = new ParentTransaction(transaction);
        return (EntityTransaction) Proxy.newProxyInstance(
                transaction.getClass().getClassLoader(),
                new Class<?>[] { EntityTransaction.class },
                tx);
    }

}
